#include "Olympus.h"
#include "SplashScreen.h"

namespace SplashScreen {

	#define WM_OUTPUTMESSAGE (WM_USER + 0x0001)

	SplashWindow* m_SplashWindow;

	VOID OLYMPUS_API Open()
	{
		if (m_SplashWindow != nullptr)
			return;
		m_SplashWindow = new SplashWindow();
	}

	VOID OLYMPUS_API Close()
	{
		return VOID OLYMPUS_API();
	}

	VOID OLYMPUS_API AddMessage(const WCHAR* message) {
		PostMessage(m_SplashWindow->GetHandle(), WM_OUTPUTMESSAGE, (WPARAM)message, 0);
	}


}




SplashWindow::SplashWindow()
	: Window(L"SplashScreen", L"SplashScreen", NULL, 600, 400)
{
	Win32::Window::RegisterNewClass();
	Win32::Window::Initialize();

}

SplashWindow::~SplashWindow()
{
}

LRESULT SplashWindow::MessageHandler(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
	switch (message)
	{
	case WM_PAINT:
	{
		PAINTSTRUCT ps;
		HDC hdc = BeginPaint(hWnd, &ps);

		Win32::Utils::AddBitmap(PerGameSettings::SplashURL(), hdc);

		SetBkMode(hdc, TRANSPARENT);
		SetTextColor(hdc, RGB(255, 255, 255));

		if (Engine::GetMode() != Engine::EngineMode::RELEASE) {
			std::wstring engineModeText = Engine::EngineModeToString() + L" Mode";
			SetTextAlign(hdc, TA_RIGHT);
			TextOut(hdc, m_Width - 15, 15, engineModeText.c_str(), wcslen(engineModeText.c_str()));
		}

		SetTextAlign(hdc, TA_CENTER);
		TextOut(hdc, m_Width / 2, 0, m_outputMessage, wcslen(m_outputMessage));

		EndPaint(hWnd, &ps);
	}
		break;
	case WM_OUTPUTMESSAGE:
	{
		WCHAR* msg = (WCHAR*)wParam;
		wcscpy_s(m_outputMessage, msg);
		RedrawWindow(GetHandle(), NULL, NULL, RDW_INVALIDATE);
	}
		break;
	default:
		break;
	}


	return ConmonMessapeHandler(hWnd, message, wParam, lParam);
}
